package vashion.azeroth.activity;

import com.alibaba.fastjson.JSONObject;
import org.springframework.util.CollectionUtils;
import vashion.azeroth.core.activity.ActiveConstant;
import vashion.azeroth.core.activity.GlobalActivityExtendInfoDO;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * 活动数据到业务数据的转换器
 * Date: 14/12/9
 * Time: 下午5:25
 */
public class ActivityDataConvert {

    /**
     * 根据频道扩展信息中的contents转换为具体对应的活动业务DO
     *
     * @param clazz 活动业务DO的类型
     * @param <T>
     * @return
     */
    public static <T> List<T> getBusniessDataFromContent(Class<T> clazz, List<GlobalActivityExtendInfoDO> globalActivityExtendInfoDOList) {
        List<T> resultList = new ArrayList<T>();

        if (CollectionUtils.isEmpty(globalActivityExtendInfoDOList)) {
            return resultList;
        }
        for (GlobalActivityExtendInfoDO extendInfoDO : globalActivityExtendInfoDOList) {
            String contents = extendInfoDO.getContents();
            JSONObject json = (JSONObject) JSONObject.parse(contents);
            if (json == null || !json.containsKey(ActiveConstant.JSON_KEY)) {
                continue;
            }
            T actDO = json.getObject(ActiveConstant.JSON_KEY, clazz);
            if (actDO != null) {
                resultList.add(actDO);
            }

        }
        return resultList;
    }

}
